// pages/searchlist/searchlist.js
var app=getApp();
var util=require("../../utils/util");
var home=require("../../api/home");


Page({
  /*页面的初始数据*/
  data: {
    info: '',
    name: '',
    search_list: [],
    bar_index: null,
    com_data: [],
    show_address: true,
    currentPage: 1,
    pageSize: 10,
    hasMoreDate: true,
    address_info: '',
    userinfo: '',
    show_nav: false
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this,name=options.name,userinfo=wx.getStorageSync('userinfo');
    that.setData({name: name,userinfo:userinfo});
    wx.getStorage({
      key: 'city',
      success:function(res){
        that.setData({address_info:res.data});
        //
        that.get_cour_list(name);
      }
    })
    if(userinfo){
      wx.getStorage({
        key: 'search_list',
        success:function(res){
          var list=res.data,search=true;
          that.setData({search_list:list})
          for(var i=0;i<list.length;i++){
            if(list[i].keyword==name){
              search=false;
            }
          }
          if(search){
            that.add_search(name,userinfo.id)
          }
        }
      })
    }
    // 事件统计
    app.td_app_sdk.event({
      id: 'search',
      label: '搜索',
      params: {
        key1: name
      }
    })
  },
  // 顶部切换
  check_bar:function(e){
    var that = this,bar_index=that.data.bar_index,name=that.data.name;
    var index=e.currentTarget.dataset.index;
    if(index!=bar_index){
      that.setData({bar_index: index,currentPage:1,com_data: [],show_nav:false})
      if(index==1){
        that.get_mech_list(name)
      }else{
        that.get_cour_list(name)
      }
    }
  },
  // 空数据
  nav_home:function(){
    var that=this,bar_index=that.data.bar_index;
    if(bar_index==1){
      wx.redirectTo({
        url: '../nearMech/nearMech',
      })
    }else{
      wx.switchTab({
        url: '../home/home',
      })
    }
  },
  // 触底
  onReachBottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,bar_index=that.data.bar_index,name=that.data.name;
    if(hasMoreData){
      if(bar_index==1){
        that.get_mech_list(name)
      }else{
        that.get_cour_list(name)
      }
    }else{
      util.page_show_toast('没有更多了',1500);
    }
  },
  //
  search_val:function(e){
    this.setData({ name: e.detail.value })
  },
  // 搜索
  search_word:function(e){
    var that = this,bar_index=that.data.bar_index,testSpace = /^[ ]*$/g,userinfo=that.data.userinfo,search_list=that.data.search_list,search=true;
    var name = e.detail.value ? e.detail.value : that.data.name;
    if(!name||testSpace.test(name)){
      util.page_show_toast('请输入关键字搜索',1500);
    }else{
      name=util.removeAllSpace(name)
      that.setData({currentPage:1,com_data: [],name:name})
      if(bar_index==1){
        that.get_mech_list(name)
      }else{
        that.get_cour_list(name)
      }
      if(userinfo){
        for(var i=0;i<search_list.length;i++){
          if(search_list[i].keyword==name){
            search=false;
          }
        }
        if(search){
          that.add_search(name,userinfo.id)
        }
      }
    }
  },
  // 获取课程列表
  get_cour_list:function(name){
    wx.showLoading({title: '加载中'})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize,address_info=that.data.address_info;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "status": 3,
      "name": name,
      "lat": address_info.lat,
      "lng": address_info.lng
    }
    home.get_cour_list(params).then(res=>{
      if(res.data.code==200){
        var list=res.data.data.records;
        if(list.length>0){
          for(var i=0;i<list.length;i++){
            list[i].introducePictures=list[i].introducePictures?list[i].introducePictures.split(","):'';
            list[i].score=list[i].score?list[i].score.toFixed(1):'5.0';
            list[i].space=list[i].distance>1000?(list[i].distance/1000).toFixed(2):list[i].distance;
            list[i].labelName=list[i].labelName?list[i].labelName.split(","):'';
            list[i].courseNatureName=list[i].courseNatureName?list[i].courseNatureName.substr(0,2):''
            list[i].sellCount=list[i].sellCount?list[i].sellCount:0
          }
        }
        var com_data = that.data.com_data;
        if(list.length==0&&com_data.length==0&&!that.data.bar_index){
          that.get_mech_list(name)
          return false;
        }else{
          that.setData({bar_index: 0})
        }
        com_data = util.extend(com_data, list)
        if (list.length < pageSize) {
          that.setData({
            com_data: com_data,
            hasMoreData: false
          })
        } else {
          that.setData({
            com_data: com_data,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
        if(list.length==0&&com_data.length==0){
          that.setData({show_nav:true})
        }else{
          that.setData({show_nav:false})
        }
      }
    })
  },
  // 获取机构列表
  get_mech_list:function(name){
    wx.showLoading({title: '加载中'})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize,address_info=that.data.address_info;
    var params={
      "lat": address_info.lat,
      "lng": address_info.lng,
      "currentPage": currentPage,
      "pageSize": pageSize,
      "status": 2,
      // "isCertified": false,
      "categoryNames": '',
      "name": name
    }
    home.get_mech_list(params).then(res=>{
      if(res.data.code==200){
        var list = res.data.data.records;
        if(list.length>0){
          for(var i=0;i<list.length;i++){
            list[i].score=list[i].score?list[i].score.toFixed(1):'5.0';
            list[i].space=list[i].distance>1000?(list[i].distance/1000).toFixed(2):list[i].distance;
          }
        }
        if(list.length==0&&!that.data.bar_index){
          that.setData({bar_index: 0,show_nav:true})
          return false;
        }else{
          that.setData({bar_index: 1})
        }
        var com_data = that.data.com_data;
        com_data = util.extend(com_data, list)
        if (list.length < pageSize) {
          that.setData({
            com_data: com_data,
            hasMoreData: false
          })
        } else {
          that.setData({
            com_data: com_data,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
        if(list.length==0&&com_data.length==0){
          that.setData({show_nav:true})
        }else{
          that.setData({show_nav:false})
        }
      }
    })
  },
  // 添加搜索词
  add_search:function(name,id){
    var params={
      "keyword": name,
      "createBy": id,
      "isDel": 0
    }
    home.add_search(params).then(res=>{

    })
  },
})